<?php
/*
*
* -------------------------------------------------------
* CLASSNAME:        product
* GENERATION DATE:  09.04.2013
* CLASS FILE:       D:\wamp\www\webpros\webpros_sites\webpros\module-generator/generated-classes/class.product.inc.php
* FOR MYSQL TABLE:  product
* FOR MYSQL DB:     modulegenerator
* -------------------------------------------------------
* -------------------------------------------------------
*
*/

class product {
/**
*   @desc Variable Declaration with default value
*/
	private $_id;   // KEY ATTR. WITH AUTOINCREMENT
     private $_cid;  
     private $_productname;  
	 private $_alias; 
     private $_quantity;  
	 private $_price;   ####
	 private $_dealerprice; ###  
	 private $_showprice;   ####
     private $_imagename;  
     private $_description;  
     private $_specification;  
	 private $_features;   #####
     private $_createddate;  
     private $_updateddate;  
     private $_status;  
	 
	 
     
/**
*   @desc   CONSTRUCTOR METHOD
*/
	function __construct() {
		global $DBOBJ;
		$this->_obj = $DBOBJ;
		$this->_id = null; 
		$this->_cid = null; 
		$this->_productname = null; 
		$this->_alias = null; 
		$this->_quantity = null;  
		$this->_price = null;  #####
		$this->_dealerprice = null;  ####
		$this->_showprice = 1;  ####
		$this->_imagename = null; 
		$this->_description = null; 
		$this->_specification = null; 
		$this->_features = null;  #####
		$this->_createddate = null; 
		$this->_updateddate = null; 
		$this->_status = null; 
	}

/**
*   @desc   DECONSTRUCTOR METHOD
*/
	function __destruct() {
		unset($this->_obj);
	}

/**
*   @desc   GETTER METHODS
*/

     public function getid() {
     	return $this->_id;
     }
     
     public function getcid() {
     	return $this->_cid;
     }
     
     public function getproductname() {
     	return $this->_productname;
     }
	 
	 
	public function getalias() {
        return $this->_alias;
    }
     
     public function getquantity() {
     	return $this->_quantity;
     }
	 
	 ########
	 public function getprice() {
     	return $this->_price;
     }
	 

	public function getdealerprice() {
     	return $this->_dealerprice;
     }
	 

	public function getshowprice() {
		return $this->_showprice;
    }
	########
     
     public function getimagename() {
     	return $this->_imagename;
     }
     
     public function getdescription() {
     	return $this->_description;
     }
     
     public function getspecification() {
     	return $this->_specification;
     }
	 
	 #######
	 public function getfeatures() {
     	return $this->_features;
     }
	 
	 
     
     public function getcreateddate() {
     	return $this->_createddate;
     }
     
     public function getupdateddate() {
     	return $this->_updateddate;
     }
     
     public function getstatus() {
     	return $this->_status;
     }
     
/**
*   @desc   SETTER METHODS
*/

     public function setid($id) {
     	 $this->_id =  $id;
     }
     
     public function setcid($cid) {
     	 $this->_cid =  $cid;
     }
     
     public function setproductname($productname) {
     	 $this->_productname =  $productname;
     }
	 
	 
	public function setalias($alias) {
        $this->_alias = $alias;
    }
     
     public function setquantity($quantity) {
     	 $this->_quantity =  $quantity;
     }
	 
	 
	 ########
	 
	 public function setprice($price) {
     	 $this->_quantity =  $price;
     }
	 
	 public function setdealerprice($dealerprice) {
     	 $this->_dealerprice =  $dealerprice;
     }
	 
	 public function setshowprice($showprice) {
     	 $this->_showprice =  $showprice;
     }
	 
	 ########
     
     public function setimagename($imagename) {
     	 $this->_imagename =  $imagename;
     }
     
     public function setdescription($description) {
     	 $this->_description =  $description;
     }
     
     public function setspecification($specification) {
     	 $this->_specification =  $specification;
     }
	 
	 ####
	 public function setfeatures($features) {
     	 $this->_features =  $features;
     }
	 
	 
     
     public function setcreateddate($createddate) {
     	 $this->_createddate =  $createddate;
     }
     
     public function setupdateddate($updateddate) {
     	 $this->_updateddate =  $updateddate;
     }
     
     public function setstatus($status) {
     	 $this->_status =  $status;
     }
     
/**
*   @desc   SELECT METHOD / LOAD
*/

	public function select($id) {
		 $sql =  "SELECT * FROM `product` WHERE `id` = '". $id ."' ";
		 $row =  $this->_obj->select($sql); 

		 if (count($row) > 0) {
			 $this->_id = $row[0]['id'];
     		 $this->_cid = $row[0]['cid'];
     		 $this->_productname = $row[0]['productname'];
			 $this->_alias = $row[0]['alias'];  
     		 $this->_quantity = $row[0]['quantity'];
			 
			 $this->_price = $row[0]['price']; ####
			 $this->_dealerprice = $row[0]['dealerprice']; ####
			 $this->_showprice = $row[0]['showprice']; ####
			 
     		 $this->_imagename = $row[0]['imagename'];
     		 $this->_description = $row[0]['description'];
     		 $this->_specification = $row[0]['specification'];
			 $this->_features = $row[0]['features']; #######
			 
     		 $this->_createddate = $row[0]['createddate'];
     		 $this->_updateddate = $row[0]['updateddate'];
     		 $this->_status = $row[0]['status'];
     			 return $row[0];
		}else {
			 return;
		}
	}
/**
*   @desc   set All
*/
	 public function setAll($dataArray = array()) {
		 if (count($dataArray) <= 0) {
			return;
		}

		foreach ($dataArray as $key => $value) {
			$classKey = '_' . $key;
			$this->$classKey = $value;
		}
	}

/**
*   @desc   get All
*/

	 public function getAll($FORMAT = 'ARRAY') {
		$dataArray = new stdClass();
		$dataArray->id = $this->getid();
     	$dataArray->cid = $this->getcid();
     	$dataArray->productname = $this->getproductname();
		$dataArray->alias = $this->getalias();  
     	$dataArray->quantity = $this->getquantity();
		
		$dataArray->price = $this->getprice(); ####
		$dataArray->dealerprice = $this->getdealerprice(); ####
		$dataArray->showprice = $this->getshowprice(); ####
		
     	$dataArray->imagename = $this->getimagename();
     	$dataArray->description = $this->getdescription();
     	$dataArray->specification = $this->getspecification();
		$dataArray->features = $this->getfeatures(); ######
     	$dataArray->createddate = $this->getcreateddate();
     	$dataArray->updateddate = $this->getupdateddate();
     	$dataArray->status = $this->getstatus();
     	if ($FORMAT == 'ARRAY') {
			return (array) $dataArray;
		}
		return $dataArray;
	}
/**
*   @desc   COUNT
*/

	public function getCountproduct() {
		 $sql = "SELECT count(*) as cnt  FROM `product`";
		 $result = $this->_obj->select($sql);
		 return $result['0']['cnt'];
	}

/**
*   @desc   DELETE
*/
	public function delete($id) {
		 $sql = "DELETE FROM `product` WHERE `id` = '". $id ."'";
		 $result = $this->_obj->sql_query($sql);
	}

/**
*   @desc   INSERT
*/
	#########
	public function insert() {
		 $this->id = ""; // clear key for autoincrement
		 $sql = "INSERT INTO `product` ( `id`,`cid`,`productname`,`alias`,`quantity`,`price`,`dealerprice`,`showprice`,`imagename`,`description`,`specification`,`features`,`createddate`,`updateddate`,`status` ) VALUES ( NULL,'".$this->_cid."','".$this->_productname."','".$this->_alias."','".$this->_quantity."','".$this->_price."','".$this->_dealerprice."','".$this->_showprice."','".$this->_imagename."','".$this->_description."','".$this->_specification."','".$this->_features."','".$this->_createddate."','".$this->_updateddate."','".$this->_status."' )";
		 $result = $this->_obj->insert($sql);
		 return $result;
	}

/**
*   @desc   UPDATE
*/
	public function update($id) {
		 $sql = " UPDATE `product` SET  `id` = '".$this->_id."' , `cid` = '".$this->_cid."' , `productname` = '".$this->_productname."', `alias` = '".$this->_alias."' , `quantity` = '".$this->_quantity."' , `price` = '".$this->_price."'  ,  `dealerprice` = '".$this->_dealerprice."'  ,  `showprice` = '".$this->_showprice."'  ,`imagename` = '".$this->_imagename."' , `description` = '".$this->_description."' , `specification` = '".$this->_specification."' ,  `features` = '".$this->_features."' , `createddate` = '".$this->_createddate."' , `updateddate` = '".$this->_updateddate."' , `status` = '".$this->_status."'  WHERE `id` =   '".$id."' ";
		 $result = $this->_obj->sql_query($sql);
	}
	public function getAllProductList($params = "*", $where = "", $orderby = "id", $limit = "") {
		$orderby = "ORDER BY " . $orderby;
		$sql = "SELECT " . $params . " FROM `product` a  " . $where . " " . $orderby . " " . $limit;
		$row = $this->_obj->select($sql);
		if ($row) {
			return $row;
		}
		return array();
	}
	
	public function performListAction($action = "", $ids = "''") {
		switch ($action) {
			case "delete":
				$idArray = explode(',', $ids);
				if (count($idArray) > 0) {
					foreach ($idArray as $_id) {
						$this->select($_id);
						$this->deleteImagename($this->getimagename());
					}
				}
				$sql = "DELETE FROM `product` WHERE `id` IN (" . $ids . ")";
			break;
			default:
				$sql = "";
		}
		if ($sql == "") {
			return false;
		}
		$result = $this->_obj->sql_query($sql);
		return true;
    }
	

	public function updateImage($id = "", $filename = "") {
		if ($id == "") {
			return false;
		}
		$updateImageQuery = "UPDATE product SET  imagename = '" . $filename . "' WHERE `id` = '" . $id . "'";
		$this->_obj->sql_query($updateImageQuery);
    }


	#############
	public function deleteImagename($name) {
		$sourceFolder = "../upload/images/product/";
		$imagepath = $sourceFolder . trim($name);
		$thumb_imagepath = $sourceFolder . trim("thumb_".$name);
		
		$medium_imagepath = $sourceFolder . trim("medium_".$name);
		
		if (file_exists($imagepath)) {
			@unlink($imagepath);
		}
		
		if (file_exists($thumb_imagepath)) {
			@unlink($thumb_imagepath);
		}
		
		if (file_exists($medium_imagepath)) {
			@unlink($medium_imagepath);
		}
	}
	#############
	
	public function checkProductAliasExists($alias) {
        $sql = "SELECT * FROM `product` WHERE `alias` = '".$alias."' limit 1";
        $row = $this->_obj->select($sql);
        if (count($row) > 0) {
            return true;
        } else {
            return false;
        }
    }
	
	
	//In the Edit mode 
	public function checkProductAlias($currentAlias = '' ,$alias) {
        $sql = "select * from product where alias = '" . $currentAlias . "' or alias = '".$alias."' ";
        $row = $this->_obj->select($sql);
        if (count($row) > 1) {
            return false;
        }
        return true;
    }
	
	
	

} 
?>